<link rel="stylesheet" href="/static/css/sweetalert.css">
    <div class="cart_content margin_top_foot">
        <header class="header fixed_top bg_mx_green"></header>
        <div class="cart_con"></div>
    </div>
    <script type="text/template" charset="utf-8" id='detail_template'>
        {{if(it.total){}}
            <div class="cart">
                {{for(var i in it){}}
                    {{if(i!=='total' && i!=='totalprice' && i!=='totalweight' && i!=="allselected" && i!=="number"){}}
                    <div class="cart_item  h_justify v_center bg_white padding_10_15 border_b">
                        <label class="width_30 selected" data-i="{{=i}}">
                            <input class="checkbox" type="checkbox" name="checkbox" {{if(it[i].selected){}}checked{{}}}>
                        </label>
                        <div class="width_70">
                            <img class="circle_5" src="{{=it[i].img}}" alt="">
                        </div>
                        <div class="left_100 padding_l_10">
                            <div class="flex_justify font_16 margin_b_10">
                                <span>{{=it[i].title}}</span>
                                <span class="iconfont icon-trash mx_green padding_h_10" onclick="del_cart({{=i}})"></span>
                            </div>
                            <div class="font_10 warning font_18">￥{{=it[i].price}}</div>
                            <div class="flex_justify">
                                <div class="light linethrough">{{if(it[i].o_price){}} {{=it[i].o_price}} {{}}}</div>
                                <div class="border flex text_center lineheight_3">
                                    <div class="padding_h_5 bg_grey padding_h_10" onclick="change_cart({{=i}},-1)"><i class="iconfont icon-del"></i></div>
                                    <div class="border_l padding_h_5 text_center padding_h_15 number">{{=it[i].num}}</div>
                                    <div class="border_l padding_h_5 bg_grey padding_h_10" onclick="change_cart({{=i}},1)"><i class="iconfont icon-add"></i></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    {{} }}
                {{} }}
            </div>
            <footer class="fixed_bottom h_justify v_center padding_h_15 border_t_mx bg_white detail_foot">
                <label class="quanx allselected">
                    <input class="checkbox" type="checkbox" name="checkbox" {{if(it.allselected){}}checked{{}}}> 全选
                </label>
                <div class="font_20 warning">￥{{=it.totalprice.toFixed(2)}}</div>
                <div class="btn btn_danger gobuy">立即结算</div>
            </footer>
        {{}else{}}
            <div class="padding_15">购物车里暂时木有宝贝，快去添加吧</div>
        {{}}}
    </script>
    <script src="/static/js/sweetalert-dev.js"></script>
    <script>
        // 购物车模版
            var carts = get_data("cart")||{total:0};
            if(typeof carts.allselected=="undefined"){
                carts.allselected = 1;
                carts.number = carts.total;
            }
            var evalcart = doT.template($("#detail_template").text());
            cal();console.log(carts)
        // 改变数量函数
            function change_cart(i,n){
                var num = carts[i].num;
                var price = carts[i].price;
                console.log(carts[i].num);
                console.log(carts[i].total);
                if(n<0 && carts[i].num==1){
                    return false;
                }else if(n >0 && carts[i].num >= carts[i].total){
                    return false;
                }
                num += n;
                if(num<=1){
                    num = 1;
                }else{
                    if(carts[i].num > carts[i].total){
                        num = carts[i].total;
                        console.log(222);
                    }
                }
                console.log(num);

                carts.total += n;
                carts[i].num = num;
                cal()
                console.log(carts.total);
            }
        // 计算总价
            function cal(){
                carts.number = 0,carts.totalprice = 0,carts.totalweight = 0;
                every(carts,function(i){
                    if(carts[i].selected){
                        var number = carts[i].num;
                        var price = carts[i].price;
                        var weight = carts[i].weight;
                        carts.number += number;
                        carts.totalprice += price*number;
                        carts.totalweight += weight*number;
                    }
                })
                loaddata(".cart_con",evalcart,carts);
                set_data(carts,"cart");
            }
        // 单选
            $(".cart_con").on("click",".selected",function(){
                var i = $(this).data("i");
                carts[i].selected = !carts[i].selected;
                carts.allselected = 1;
                every(carts,function(i){
                    if(!carts[i].selected){
                        carts.allselected = 0;
                    }
                })
                cal();
            })
        // 全选
            $(".cart_con").on("click",".allselected",function(){
                carts.allselected = !carts.allselected;
                every(carts,function(i){
                    carts[i].selected = carts.allselected;
                })
                cal();
            })
        // 遍历函数
            function every(data,callback){
                for (var i in carts) {
                    if(i !== "total" && i!=="totalprice" && i!=="totalweight" && i!=="allselected" && i!=="number"){
                        callback(i)
                    }
                }
            }
        // 删除购物车
            function del_cart(n){

                // var carts = get_data("cart")||{total:0};
                // console.log(carts);
                // if(confirm("确定删除吗？")){
                //     carts.total -= carts[n].num;
                //     delete carts[n];
                //     cal();
                //     set_data(carts,"cart");console.log(carts)
                // }
                // if(confirm("确定删除吗？")){
                //     carts.total -= carts[n].num;
                //     delete carts[n];
                //     cal();
                //     set_data(carts,"cart");console.log(carts)
                // }
                swal({
                        title: "确定删除吗？",
                        text: "",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "确定删除！",
                        cancelButtonText: "取消删除！",
                        closeOnConfirm: true,
                        closeOnCancel: true
                    },
                    function(isConfirm){
                        if (isConfirm) {
                            carts.total -= carts[n].num;
                            delete carts[n];
                            cal();
                            set_data(carts,"cart");
                            console.log(carts)
                        }
                    });
            }
        // 结算
            $(".cart_con").on("click",".gobuy",function(){
                if(carts.number){
                    location.href="/shop/order/confirm";
                }else{
                    alert("购物车里暂时木有宝贝，快去添加吧")
                }
            })
        // 头部模版引入
            loadheadbar("购物车");
    </script>


